<?php
namespace app\admin\controller;
use app\common\model;
use think\Exception;

class Base extends \app\common\controller\Base {
    public $admininfo;
    public $menuModel;
    public $role_id;
    public $manager_id;
    public $managerModel;
    public $roleModel;
    public $configModel;
    public function __construct() {
        parent::__construct();
        $this->menuModel = new model\Menu();
        $this->managerModel = new model\Manager;
        $this->roleModel = new model\Role;
        $this->configModel = new model\Config;
        $this->_setConfig();
        $this->_checkLogin();
        $this->_checkPass();
        $this->_setMenu();

        $this->role_id = $this->admininfo['role_id'];
        $this->manager_id = $this->admininfo['id'];
        define('IS_AJAX',request()->isAjax());
    }
    
    protected function _checkLogin () {
        try{
            $admininfo = session('admininfo');
            $controller = strtolower(request()->controller());
            $action = strtolower(request()->action());
            $this->assign('controller',$controller);
            $this->assign('action',$action);
            if($controller=='login') {
                return true;
            }
            if(!$admininfo) {
                $this->redirect('/admin/login');
            }else{
                $admininfo=$this->managerModel->getOne(['id'=>$admininfo['id']]);
                if($admininfo){
                    $this->assign('admininfo',$admininfo);
                    session('admininfo',$admininfo);
                    $this->admininfo = $admininfo;
                }else{
                    session_destroy();
                    $this->redirect('/admin/login');
                }
            }
            return true;
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }
    }
    
    protected function _setConfig () {
        try{
            $list = $this->configModel->getList();
            foreach ($list as $k=>$v) {
                config($v['key'], $v['value']);
            }
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
        }
    }
    
    protected function _setMenu () {
        try{
            $role = $this->roleModel->getOne(['id'=>$this->admininfo['role_id']]);
            $menuwhere = [
                    'status' => 1,
                    'level' => 1,
                    'id' => array('in',$role['menu'])
            ];
            if($role['menu']=='all'){
                unset($menuwhere['id']);
            }
            $menus = $this->menuModel->getList($menuwhere,'ord desc');
            foreach ($menus as $k=>$menu) {
                $subwhere = [
                    'pid' => $menu['id'],
                    'status' => 1,
                    'level' => 2,
                    'id' => array('in',$role['menu'])
                ];
                if($role['menu']=='all'){
                    unset($subwhere['id']);
                }
                $menus[$k]['menus'] = db('menu')->where($subwhere)->select();
            }
            $this->assign('menus',$menus);
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
        }
    }
    
    protected function _checkPass () {
        try{
            $role = $this->roleModel->getOne(['id'=>$this->admininfo['role_id']]);
            $where = [
                'status' => 1,
//            'id' => array('in',$role['menu'])
            ];
            $whiteList=self::whiteList();
            $menus = $this->menuModel->getList($where);
            $controller = strtolower(request()->controller());
            $action = strtolower(request()->action());

            foreach ($whiteList as $k=>$v) {
                if($k==$controller&&in_array($action,$v)) {
                    return true;
                }
            }

            foreach ($menus as $v) {
                if($controller==$v['controller']&&$action==$v['action']) {
                    return true;
                }
            }
            if(request()->isAjax()) {
                header("content-type:application/json");
                exit(json_encode([
                    'ret' => 1,
                    'msg' => '无权访问'
                ], true));
            }
            return false;
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }

    }

    public function whiteList(){
        $listArray =array(
            'meeting'=>array('createmeeting'),//控制器对应方法
        );
        return $listArray;
    }
}